home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / var / lib / dpkg / info / xserver-xorg.postrm < prev    next >
Encoding:
Text File  |  2009-04-03  |  33.0 KB  |  1,024 lines

  1. #!/bin/sh
  2. # Debian xserver-xorg package post-removal script
  3. # Copyright 1998--2001, 2003 Branden Robinson.
  4. # Licensed under the GNU General Public License, version 2.  See the file
  5. # /usr/share/common-licenses/GPL or <http://www.gnu.org/copyleft/gpl.txt>.
  6. # Acknowledgements to Stephen Early, Mark Eichin, and Manoj Srivastava.
  7.  
  8.  
  9. set -e
  10.  
  11. THIS_PACKAGE=xserver-xorg
  12. THIS_SCRIPT=postrm
  13.  
  14. # $Id$
  15.  
  16. # This is the X Strike Force shell library for X Window System package
  17. # maintainer scripts.  It serves to define shell functions commonly used by
  18. # such packages, and performs some error checking necessary for proper operation
  19. # of those functions.  By itself, it does not "do" much; the maintainer scripts
  20. # invoke the functions defined here to accomplish package installation and
  21. # removal tasks.
  22.  
  23. # If you are reading this within a Debian package maintainer script (e.g.,
  24. # /var/lib/dpkg)info/PACKAGE.{config,preinst,postinst,prerm,postrm}), you can
  25. # skip past this library by scanning forward in this file to the string
  26. # "GOBSTOPPER".
  27.  
  28. SOURCE_VERSION=1:7.4~5ubuntu18
  29. OFFICIAL_BUILD=
  30.  
  31. # Use special abnormal exit codes so that problems with this library are more
  32. # easily tracked down.
  33. SHELL_LIB_INTERNAL_ERROR=86
  34. SHELL_LIB_THROWN_ERROR=74
  35. SHELL_LIB_USAGE_ERROR=99
  36.  
  37. # old -> new variable names
  38. if [ -z "$DEBUG_XORG_PACKAGE" ] && [ -n "$DEBUG_XFREE86_PACKAGE" ]; then
  39.   DEBUG_XORG_PACKAGE="$DEBUG_XFREE86_PACKAGE"
  40. fi
  41. if [ -z "$DEBUG_XORG_DEBCONF" ] && [ -n "$DEBUG_XFREE86_DEBCONF" ]; then
  42.   DEBUG_XORG_DEBCONF="$DEBUG_XFREE86_DEBCONF"
  43. fi
  44.  
  45. # initial sanity checks
  46. if [ -z "$THIS_PACKAGE" ]; then
  47.   cat >&2 <<EOF
  48. Error: package maintainer script attempted to use shell library without
  49. definining \$THIS_PACKAGE shell variable.  Please report the package name,
  50. version, and the text of this error message to the Debian Bug Tracking System.
  51. Visit <http://www.debian.org/Bugs/Reporting> on the World Wide Web for
  52. instructions, read the file /usr/share/doc/debian/bug-reporting.txt from the
  53. "doc-debian" package, or install the "reportbug" package and use the command of
  54. the same name to file a report against version $SOURCE_VERSION of this package.
  55. EOF
  56.   exit $SHELL_LIB_USAGE_ERROR
  57. fi
  58.  
  59. if [ -z "$THIS_SCRIPT" ]; then
  60.   cat >&2 <<EOF
  61. Error: package maintainer script attempted to use shell library without
  62. definining \$THIS_SCRIPT shell variable.  Please report the package name,
  63. version, and the text of this error message to the Debian Bug Tracking System.
  64. Visit <http://www.debian.org/Bugs/Reporting> on the World Wide Web for
  65. instructions, read the file /usr/share/doc/debian/bug-reporting.txt from the
  66. "doc-debian" package, or install the "reportbug" package and use the command of
  67. the same name to file a report against version $SOURCE_VERSION of the
  68. "$THIS_PACKAGE" package.
  69. EOF
  70.   exit $SHELL_LIB_USAGE_ERROR
  71. fi
  72.  
  73. ARCHITECTURE="$(dpkg --print-installation-architecture)"
  74.  
  75. if [ "$1" = "reconfigure" ] || [ -n "$DEBCONF_RECONFIGURE" ]; then
  76.   RECONFIGURE="true"
  77. else
  78.   RECONFIGURE=
  79. fi
  80.  
  81. if ([ "$1" = "install" ] || [ "$1" = "configure" ]) && [ -z "$2" ]; then
  82.   FIRSTINST="yes"
  83. fi
  84.  
  85. if [ -z "$RECONFIGURE" ] && [ -z "$FIRSTINST" ]; then
  86.   UPGRADE="yes"
  87. fi
  88.  
  89. trap "message;\
  90.       message \"Received signal.  Aborting $THIS_PACKAGE package $THIS_SCRIPT script.\";\
  91.       message;\
  92.       exit 1" HUP INT QUIT TERM
  93.  
  94. reject_nondigits () {
  95.   # syntax: reject_nondigits [ operand ... ]
  96.   #
  97.   # scan operands (typically shell variables whose values cannot be trusted) for
  98.   # characters other than decimal digits and barf if any are found
  99.   while [ -n "$1" ]; do
  100.     # does the operand contain anything but digits?
  101.     if ! expr "$1" : "[[:digit:]]\+$" > /dev/null 2>&1; then
  102.       # can't use die(), because it wraps message() which wraps this function
  103.       echo "$THIS_PACKAGE $THIS_SCRIPT error: reject_nondigits() encountered" \
  104.            "possibly malicious garbage \"$1\"" >&2
  105.       exit $SHELL_LIB_THROWN_ERROR
  106.     fi
  107.     shift
  108.   done
  109. }
  110.  
  111. reject_whitespace () {
  112.   # syntax: reject_whitespace [ operand ]
  113.   #
  114.   # scan operand (typically a shell variable whose value cannot be trusted) for
  115.   # whitespace characters and barf if any are found
  116.   if [ -n "$1" ]; then
  117.     # does the operand contain any whitespace?
  118.     if expr "$1" : "[[:space:]]" > /dev/null 2>&1; then
  119.       # can't use die(), because I want to avoid forward references
  120.       echo "$THIS_PACKAGE $THIS_SCRIPT error: reject_whitespace() encountered" \
  121.            "possibly malicious garbage \"$1\"" >&2
  122.       exit $SHELL_LIB_THROWN_ERROR
  123.     fi
  124.   fi
  125. }
  126.  
  127. reject_unlikely_path_chars () {
  128.   # syntax: reject_unlikely_path_chars [ operand ... ]
  129.   #
  130.   # scan operands (typically shell variables whose values cannot be trusted) for
  131.   # characters unlikely to be seen in a path and which the shell might
  132.   # interpret and barf if any are found
  133.   while [ -n "$1" ]; do
  134.     # does the operand contain any funny characters?
  135.     if expr "$1" : '.*[!$&()*;<>?|].*' > /dev/null 2>&1; then
  136.       # can't use die(), because I want to avoid forward references
  137.       echo "$THIS_PACKAGE $THIS_SCRIPT error: reject_unlikely_path_chars()" \
  138.            "encountered possibly malicious garbage \"$1\"" >&2
  139.       exit $SHELL_LIB_THROWN_ERROR
  140.     fi
  141.     shift
  142.   done
  143. }
  144.  
  145. # Query the terminal to establish a default number of columns to use for
  146. # displaying messages to the user.  This is used only as a fallback in the
  147. # event the COLUMNS variable is not set.  ($COLUMNS can react to SIGWINCH while
  148. # the script is running, and this cannot, only being calculated once.)
  149. DEFCOLUMNS=$(stty size 2> /dev/null | awk '{print $2}') || true
  150. if ! expr "$DEFCOLUMNS" : "[[:digit:]]\+$" > /dev/null 2>&1; then
  151.   DEFCOLUMNS=80
  152. fi
  153.  
  154. message () {
  155.   # pretty-print messages of arbitrary length
  156.   reject_nondigits "$COLUMNS"
  157.   echo "$*" | fmt -t -w ${COLUMNS:-$DEFCOLUMNS} >&2
  158. }
  159.  
  160. observe () {
  161.   # syntax: observe message ...
  162.   #
  163.   # issue observational message suitable for logging someday when support for
  164.   # it exists in dpkg
  165.   if [ -n "$DEBUG_XORG_PACKAGE" ]; then
  166.     message "$THIS_PACKAGE $THIS_SCRIPT note: $*"
  167.   fi
  168. }
  169.  
  170. warn () {
  171.   # syntax: warn message ...
  172.   #
  173.   # issue warning message suitable for logging someday when support for
  174.   # it exists in dpkg; also send to standard error
  175.   message "$THIS_PACKAGE $THIS_SCRIPT warning: $*"
  176. }
  177.  
  178. die () {
  179.   # syntax: die message ...
  180.   #
  181.   # exit script with error message
  182.   message "$THIS_PACKAGE $THIS_SCRIPT error: $*"
  183.   exit $SHELL_LIB_THROWN_ERROR
  184. }
  185.  
  186. internal_error () {
  187.   # exit script with error; essentially a "THIS SHOULD NEVER HAPPEN" message
  188.   message "internal error: $*"
  189.   if [ -n "$OFFICIAL_BUILD" ]; then
  190.     message "Please report a bug in the $THIS_SCRIPT script of the" \
  191.             "$THIS_PACKAGE package, version $SOURCE_VERSION to the Debian Bug" \
  192.             "Tracking System.  Include all messages above that mention the" \
  193.             "$THIS_PACKAGE package.  Visit " \
  194.             "<http://www.debian.org/Bugs/Reporting> on the World Wide Web for" \
  195.             "instructions, read the file" \
  196.             "/usr/share/doc/debian/bug-reporting.txt from the doc-debian" \
  197.             "package, or install the reportbug package and use the command of" \
  198.             "the same name to file a report."
  199.   fi
  200.   exit $SHELL_LIB_INTERNAL_ERROR
  201. }
  202.  
  203. usage_error () {
  204.   message "usage error: $*"
  205.   message "Please report a bug in the $THIS_SCRIPT script of the" \
  206.           "$THIS_PACKAGE package, version $SOURCE_VERSION to the Debian Bug" \
  207.           "Tracking System.  Include all messages above that mention the" \
  208.           "$THIS_PACKAGE package.  Visit " \
  209.           "<http://www.debian.org/Bugs/Reporting> on the World Wide Web for" \
  210.           "instructions, read the file" \
  211.           "/usr/share/doc/debian/bug-reporting.txt from the doc-debian" \
  212.           "package, or install the reportbug package and use the command of" \
  213.           "the same name to file a report."
  214.   exit $SHELL_LIB_USAGE_ERROR
  215. }
  216.  
  217.  
  218. maplink () {
  219.   # returns what symlink should point to; i.e., what the "sane" answer is
  220.   # Keep this in sync with the debian/*.links files.
  221.   # This is only needed for symlinks to directories.
  222.   #
  223.   # XXX: Most of these look wrong in the X11R7 world and need to be fixed.
  224.   # If we've stopped using this function, fixing it might enable us to re-enable
  225.   # it again and catch more errors.
  226.   case "$1" in
  227.     /etc/X11/xkb/compiled) echo /var/lib/xkb ;;
  228.     /etc/X11/xkb/xkbcomp) echo /usr/X11R6/bin/xkbcomp ;;
  229.     /usr/X11R6/lib/X11/app-defaults) echo /etc/X11/app-defaults ;;
  230.     /usr/X11R6/lib/X11/fs) echo /etc/X11/fs ;;
  231.     /usr/X11R6/lib/X11/lbxproxy) echo /etc/X11/lbxproxy ;;
  232.     /usr/X11R6/lib/X11/proxymngr) echo /etc/X11/proxymngr ;;
  233.     /usr/X11R6/lib/X11/rstart) echo /etc/X11/rstart ;;
  234.     /usr/X11R6/lib/X11/twm) echo /etc/X11/twm ;;
  235.     /usr/X11R6/lib/X11/xdm) echo /etc/X11/xdm ;;
  236.     /usr/X11R6/lib/X11/xinit) echo /etc/X11/xinit ;;
  237.     /usr/X11R6/lib/X11/xkb) echo /etc/X11/xkb ;;
  238.     /usr/X11R6/lib/X11/xserver) echo /etc/X11/xserver ;;
  239.     /usr/X11R6/lib/X11/xsm) echo /etc/X11/xsm ;;
  240.     /usr/bin/X11) echo ../X11R6/bin ;;
  241.     /usr/bin/rstartd) echo ../X11R6/bin/rstartd ;;
  242.     /usr/include/X11) echo ../X11R6/include/X11 ;;
  243.     /usr/lib/X11) echo ../X11R6/lib/X11 ;;
  244.     *) internal_error "maplink() called with unknown path \"$1\"" ;;
  245.   esac
  246. }
  247.  
  248. analyze_path () {
  249.   # given a supplied set of pathnames, break each one up by directory and do an
  250.   # ls -dl on each component, cumulatively; i.e.
  251.   # analyze_path /usr/X11R6/bin -> ls -dl /usr /usr/X11R6 /usr/X11R6/bin
  252.   # Thanks to Randolph Chung for this clever hack.
  253.  
  254.   local f g
  255.  
  256.   while [ -n "$1" ]; do
  257.     reject_whitespace "$1"
  258.     g=
  259.     message "Analyzing $1:"
  260.     for f in $(echo "$1" | tr / \  ); do
  261.       if [ -e /$g$f ]; then
  262.         ls -dl /$g$f /$g$f.dpkg-* 2> /dev/null || true
  263.         g=$g$f/
  264.       else
  265.         message "/$g$f: nonexistent; directory contents of /$g:"
  266.         ls -l /$g
  267.         break
  268.       fi
  269.     done
  270.     shift
  271.   done
  272. }
  273.  
  274. find_culprits () {
  275.   local f p dpkg_info_dir possible_culprits smoking_guns bad_packages package \
  276.     msg
  277.  
  278.   reject_whitespace "$1"
  279.   message "Searching for overlapping packages..."
  280.   dpkg_info_dir=/var/lib/dpkg/info
  281.   if [ -d $dpkg_info_dir ]; then
  282.     if [ "$(echo $dpkg_info_dir/*.list)" != "$dpkg_info_dir/*.list" ]; then
  283.       possible_culprits=$(ls -1 $dpkg_info_dir/*.list | egrep -v \
  284.         "(xbase-clients|x11-common|xfs|xlibs)")
  285.       if [ -n "$possible_culprits" ]; then
  286.         smoking_guns=$(grep -l "$1" $possible_culprits || true)
  287.         if [ -n "$smoking_guns" ]; then
  288.           bad_packages=$(printf "\\n")
  289.           for f in $smoking_guns; do
  290.             # too bad you can't nest parameter expansion voodoo
  291.             p=${f%*.list}      # strip off the trailing ".list"
  292.             package=${p##*/}   # strip off the directories
  293.             bad_packages=$(printf "%s\n%s" "$bad_packages" "$package")
  294.           done
  295.           msg=$(cat <<EOF
  296. The following packages appear to have file overlaps with the X.Org packages;
  297. these packages are either very old, or in violation of Debian Policy.  Try
  298. upgrading each of these packages to the latest available version if possible:
  299. for example, with the command "apt-get install".  If no newer version of a
  300. package is available, you will have to remove it; for example, with the command
  301. "apt-get remove".  If even the latest available version of the package has
  302. this file overlap, please file a bug against that package with the Debian Bug
  303. Tracking System.  You may want to refer the package maintainer to section 12.8
  304. of the Debian Policy manual.
  305. EOF
  306. )
  307.           message "$msg"
  308.           message "The overlapping packages are: $bad_packages"
  309.         else
  310.           message "no overlaps found."
  311.         fi
  312.       fi
  313.     else
  314.       message "cannot search; no matches for $dpkg_info_dir/*.list."
  315.     fi
  316.   else
  317.     message "cannot search; $dpkg_info_dir does not exist."
  318.   fi
  319. }
  320.  
  321. # we require a readlink command or shell function
  322. if ! which readlink > /dev/null 2>&1; then
  323.   message "The readlink command was not found.  Please install version" \
  324.           "1.13.1 or later of the debianutils package."
  325.   readlink () {
  326.     # returns what symlink in $1 actually points to
  327.     perl -e '$l = shift; exit 1 unless -l $l; $r = readlink $l; exit 1 unless $r; print "$r\n"' "$1"
  328.   }
  329. fi
  330.  
  331. check_symlink () {
  332.   # syntax: check_symlink symlink
  333.   #
  334.   # See if specified symlink points where it is supposed to.  Return 0 if it
  335.   # does, and 1 if it does not.
  336.   #
  337.   # Primarily used by check_symlinks_and_warn() and check_symlinks_and_bomb().
  338.  
  339.   local symlink
  340.  
  341.   # validate arguments
  342.   if [ $# -ne 1 ]; then
  343.     usage_error "check_symlink() called with wrong number of arguments;" \
  344.                 "expected 1, got $#"
  345.     exit $SHELL_LIB_USAGE_ERROR
  346.   fi
  347.  
  348.   symlink="$1"
  349.  
  350.   if [ "$(maplink "$symlink")" = "$(readlink "$symlink")" ]; then
  351.     return 0
  352.   else
  353.     return 1
  354.   fi
  355. }
  356.  
  357. check_symlinks_and_warn () {
  358.   # syntax: check_symlinks_and_warn symlink ...
  359.   #
  360.   # For each argument, check for symlink sanity, and warn if it isn't sane.
  361.   #
  362.   # Call this function from a preinst script in the event $1 is "upgrade" or
  363.   # "install".
  364.  
  365.   local errmsg symlink
  366.  
  367.   # validate arguments
  368.   if [ $# -lt 1 ]; then
  369.     usage_error "check_symlinks_and_warn() called with wrong number of" \
  370.                 "arguments; expected at least 1, got $#"
  371.     exit $SHELL_LIB_USAGE_ERROR
  372.   fi
  373.  
  374.   while [ -n "$1" ]; do
  375.     symlink="$1"
  376.     if [ -L "$symlink" ]; then
  377.       if ! check_symlink "$symlink"; then
  378.         observe "$symlink symbolic link points to wrong location" \
  379.                 "$(readlink "$symlink"); removing"
  380.         rm "$symlink"
  381.       fi
  382.     elif [ -e "$symlink" ]; then
  383.       errmsg="$symlink exists and is not a symbolic link; this package cannot"
  384.       errmsg="$errmsg be installed until this"
  385.       if [ -f "$symlink" ]; then
  386.         errmsg="$errmsg file"
  387.       elif [ -d "$symlink" ]; then
  388.         errmsg="$errmsg directory"
  389.       else
  390.         errmsg="$errmsg thing"
  391.       fi
  392.       errmsg="$errmsg is removed"
  393.       die "$errmsg"
  394.     fi
  395.     shift
  396.   done
  397. }
  398.  
  399. check_symlinks_and_bomb () {
  400.   # syntax: check_symlinks_and_bomb symlink ...
  401.   #
  402.   # For each argument, check for symlink sanity, and bomb if it isn't sane.
  403.   #
  404.   # Call this function from a postinst script.
  405.  
  406.   local problem symlink
  407.  
  408.   # validate arguments
  409.   if [ $# -lt 1 ]; then
  410.     usage_error "check_symlinks_and_bomb() called with wrong number of"
  411.                 "arguments; expected at least 1, got $#"
  412.     exit $SHELL_LIB_USAGE_ERROR
  413.   fi
  414.  
  415.   while [ -n "$1" ]; do
  416.     problem=
  417.     symlink="$1"
  418.     if [ -L "$symlink" ]; then
  419.       if ! check_symlink "$symlink"; then
  420.         problem=yes
  421.         warn "$symlink symbolic link points to wrong location" \
  422.              "$(readlink "$symlink")"
  423.       fi
  424.     elif [ -e "$symlink" ]; then
  425.       problem=yes
  426.       warn "$symlink is not a symbolic link"
  427.     else
  428.       problem=yes
  429.       warn "$symlink symbolic link does not exist"
  430.     fi
  431.     if [ -n "$problem" ]; then
  432.       analyze_path "$symlink" "$(readlink "$symlink")"
  433.       find_culprits "$symlink"
  434.       die "bad symbolic links on system"
  435.     fi
  436.     shift
  437.   done
  438. }
  439.  
  440. font_update () {
  441.   # run $UPDATECMDS in $FONTDIRS
  442.  
  443.   local dir cmd shortcmd x_font_dir_prefix
  444.  
  445.   x_font_dir_prefix="/usr/share/fonts/X11"
  446.  
  447.   if [ -z "$UPDATECMDS" ]; then
  448.     usage_error "font_update() called but \$UPDATECMDS not set"
  449.   fi
  450.   if [ -z "$FONTDIRS" ]; then
  451.     usage_error "font_update() called but \$FONTDIRS not set"
  452.   fi
  453.  
  454.   reject_unlikely_path_chars "$UPDATECMDS"
  455.   reject_unlikely_path_chars "$FONTDIRS"
  456.  
  457.   for dir in $FONTDIRS; do
  458.     if [ -d "$x_font_dir_prefix/$dir" ]; then
  459.       for cmd in $UPDATECMDS; do
  460.         if which "$cmd" > /dev/null 2>&1; then
  461.           shortcmd=${cmd##*/}
  462.           observe "running $shortcmd in $dir font directory"
  463.       cmd_opts=
  464.           if [ "$shortcmd" = "update-fonts-alias" ]; then
  465.             cmd_opts=--x11r7-layout
  466.           fi
  467.           if [ "$shortcmd" = "update-fonts-dir" ]; then
  468.             cmd_opts=--x11r7-layout
  469.           fi
  470.           if [ "$shortcmd" = "update-fonts-scale" ]; then
  471.             cmd_opts=--x11r7-layout
  472.           fi
  473.           $cmd $cmd_opts $dir || warn "$cmd $cmd_opts $dir" \
  474.                               "failed; font directory data may not" \
  475.                               "be up to date"
  476.         else
  477.           warn "$cmd not found; not updating corresponding $dir font" \
  478.                "directory data"
  479.         fi
  480.       done
  481.     else
  482.       warn "$dir is not a directory; not updating font directory data"
  483.     fi
  484.   done
  485. }
  486.  
  487. remove_conffile_prepare () {
  488.   # syntax: remove_conffile_prepare filename official_md5sum ...
  489.   #
  490.   # Check a conffile "filename" against a list of canonical MD5 checksums.
  491.   # If the file's current MD5 checksum matches one of the "official_md5sum"
  492.   # operands provided, then prepare the conffile for removal from the system.
  493.   # We defer actual deletion until the package is configured so that we can
  494.   # roll this operation back if package installation fails.
  495.   #
  496.   # Call this function from a preinst script in the event $1 is "upgrade" or
  497.   # "install" and verify $2 to ensure the package is being upgraded from a
  498.   # version (or installed over a version removed-but-not-purged) prior to the
  499.   # one in which the conffile was obsoleted.
  500.  
  501.   local conffile current_checksum
  502.  
  503.   # validate arguments
  504.   if [ $# -lt 2 ]; then
  505.     usage_error "remove_conffile_prepare() called with wrong number of" \
  506.                 "arguments; expected at least 2, got $#"
  507.     exit $SHELL_LIB_USAGE_ERROR
  508.   fi
  509.  
  510.   conffile="$1"
  511.   shift
  512.  
  513.   # does the conffile even exist?
  514.   if [ -e "$conffile" ]; then
  515.     # calculate its checksum
  516.     current_checksum=$(md5sum < "$conffile" | sed 's/[[:space:]].*//')
  517.     # compare it to each supplied checksum
  518.     while [ -n "$1" ]; do
  519.       if [ "$current_checksum" = "$1" ]; then
  520.         # we found a match; move the confffile and stop looking
  521.         observe "preparing obsolete conffile $conffile for removal"
  522.         mv "$conffile" "$conffile.$THIS_PACKAGE-tmp"
  523.         break
  524.       fi
  525.       shift
  526.     done
  527.   fi
  528. }
  529.  
  530. remove_conffile_lookup () {
  531.   # syntax: remove_conffile_lookup package filename
  532.   #
  533.   # Lookup the md5sum of a conffile in dpkg's database, and prepare for removal
  534.   # if it matches the actual file's md5sum.
  535.   #
  536.   # Call this function when you would call remove_conffile_prepare but only
  537.   # want to check against dpkg's status database instead of known checksums.
  538.  
  539.   local package conffile old_md5sum
  540.  
  541.   # validate arguments
  542.   if [ $# -ne 2 ]; then
  543.     usage_error "remove_conffile_lookup() called with wrong number of" \
  544.                 "arguments; expected 1, got $#"
  545.     exit $SHELL_LIB_USAGE_ERROR
  546.   fi
  547.  
  548.   package="$1"
  549.   conffile="$2"
  550.  
  551.   if ! [ -e "$conffile" ]; then
  552.     return
  553.   fi
  554.   old_md5sum="$(dpkg-query -W -f='${Conffiles}' "$package" | \
  555.     awk '{ if (match($0, "^ '"$conffile"' ")) print $2}')"
  556.   if [ -n "$old_md5sum" ]; then
  557.     remove_conffile_prepare "$conffile" "$old_md5sum"
  558.   fi
  559. }
  560.  
  561. remove_conffile_commit () {
  562.   # syntax: remove_conffile_commit filename
  563.   #
  564.   # Complete the removal of a conffile "filename" that has become obsolete.
  565.   #
  566.   # Call this function from a postinst script after having used
  567.   # remove_conffile_prepare() in the preinst.
  568.  
  569.   local conffile
  570.  
  571.   # validate arguments
  572.   if [ $# -ne 1 ]; then
  573.     usage_error "remove_conffile_commit() called with wrong number of" \
  574.                 "arguments; expected 1, got $#"
  575.     exit $SHELL_LIB_USAGE_ERROR
  576.   fi
  577.  
  578.   conffile="$1"
  579.  
  580.   # if the temporary file created by remove_conffile_prepare() exists, remove it
  581.   if [ -e "$conffile.$THIS_PACKAGE-tmp" ]; then
  582.     observe "committing removal of obsolete conffile $conffile"
  583.     rm "$conffile.$THIS_PACKAGE-tmp"
  584.   fi
  585. }
  586.  
  587. remove_conffile_rollback () {
  588.   # syntax: remove_conffile_rollback filename
  589.   #
  590.   # Roll back the removal of a conffile "filename".
  591.   #
  592.   # Call this function from a postrm script in the event $1 is "abort-upgrade"
  593.   # or "abort-install" is  after having used remove_conffile_prepare() in the
  594.   # preinst.
  595.  
  596.   local conffile
  597.  
  598.   # validate arguments
  599.   if [ $# -ne 1 ]; then
  600.     usage_error "remove_conffile_rollback() called with wrong number of" \
  601.                 "arguments; expected 1, got $#"
  602.     exit $SHELL_LIB_USAGE_ERROR
  603.   fi
  604.  
  605.   conffile="$1"
  606.  
  607.   # if the temporary file created by remove_conffile_prepare() exists, move it
  608.   # back
  609.   if [ -e "$conffile.$THIS_PACKAGE-tmp" ]; then
  610.     observe "rolling back removal of obsolete conffile $conffile"
  611.     mv "$conffile.$THIS_PACKAGE-tmp" "$conffile"
  612.   fi
  613. }
  614.  
  615. replace_conffile_with_symlink_prepare () {
  616.   # syntax: replace_conffile_with_symlink_prepare oldfilename newfilename \
  617.   # official_md5sum ...
  618.   #
  619.   # Check a conffile "oldfilename" against a list of canonical MD5 checksums.
  620.   # If the file's current MD5 checksum matches one of the "official_md5sum"
  621.   # operands provided, then prepare the conffile for removal from the system.
  622.   # We defer actual deletion until the package is configured so that we can
  623.   # roll this operation back if package installation fails. Otherwise copy it
  624.   # to newfilename and let dpkg handle it through conffiles mechanism.
  625.   #
  626.   # Call this function from a preinst script in the event $1 is "upgrade" or
  627.   # "install" and verify $2 to ensure the package is being upgraded from a
  628.   # version (or installed over a version removed-but-not-purged) prior to the
  629.   # one in which the conffile was obsoleted.
  630.  
  631.   local conffile current_checksum
  632.  
  633.   # validate arguments
  634.   if [ $# -lt 3 ]; then
  635.     usage_error "replace_conffile_with_symlink_prepare() called with wrong" \
  636.                 " number of arguments; expected at least 3, got $#"
  637.     exit $SHELL_LIB_USAGE_ERROR
  638.   fi
  639.  
  640.   oldconffile="$1"
  641.   shift
  642.   newconffile="$1"
  643.   shift
  644.  
  645.   remove_conffile_prepare "$_oldconffile" "$@"
  646.   # If $oldconffile still exists, then md5sums didn't match.
  647.   # Copy it to new one.
  648.   if [ -f "$oldconffile" ]; then
  649.     cp "$oldconffile" "$newconffile"
  650.   fi
  651.  
  652. }
  653.  
  654. replace_conffile_with_symlink_commit () {
  655.   # syntax: replace_conffile_with_symlink_commit oldfilename
  656.   #
  657.   # Complete the removal of a conffile "oldfilename" that has been
  658.   # replaced by a symlink.
  659.   #
  660.   # Call this function from a postinst script after having used
  661.   # replace_conffile_with_symlink_prepare() in the preinst.
  662.  
  663.   local conffile
  664.  
  665.   # validate arguments
  666.   if [ $# -ne 1 ]; then
  667.     usage_error "replace_conffile_with_symlink_commit() called with wrong" \
  668.                 "number of arguments; expected 1, got $#"
  669.     exit $SHELL_LIB_USAGE_ERROR
  670.   fi
  671.  
  672.   conffile="$1"
  673.  
  674.   remove_conffile_commit "$conffile"
  675. }
  676.  
  677. replace_conffile_with_symlink_rollback () {
  678.   # syntax: replace_conffile_with_symlink_rollback oldfilename newfilename
  679.   #
  680.   # Roll back the replacing of a conffile "oldfilename" with symlink to
  681.   # "newfilename".
  682.   #
  683.   # Call this function from a postrm script in the event $1 is "abort-upgrade"
  684.   # or "abort-install" and verify $2 to ensure the package failed to upgrade
  685.   # from a version (or install over a version removed-but-not-purged) prior
  686.   # to the one in which the conffile was obsoleted.
  687.   # You should have  used replace_conffile_with_symlink_prepare() in the
  688.   # preinst.
  689.  
  690.   local conffile
  691.  
  692.   # validate arguments
  693.   if [ $# -ne 2 ]; then
  694.     usage_error "replace_conffile_with_symlink_rollback() called with wrong" \
  695.                 "number of arguments; expected 2, got $#"
  696.     exit $SHELL_LIB_USAGE_ERROR
  697.   fi
  698.  
  699.   oldconffile="$1"
  700.   newconffile="$2"
  701.  
  702.   remove_conffile_rollback "$_oldconffile"
  703.   if [ -f "$newconffile" ]; then
  704.     rm "$newconffile"
  705.   fi
  706. }
  707.  
  708. run () {
  709.   # syntax: run command [ argument ... ]
  710.   #
  711.   # Run specified command with optional arguments and report its exit status.
  712.   # Useful for commands whose exit status may be nonzero, but still acceptable,
  713.   # or commands whose failure is not fatal to us.
  714.   #
  715.   # NOTE: Do *not* use this function with db_get or db_metaget commands; in
  716.   # those cases the return value of the debconf command *must* be checked
  717.   # before the string returned by debconf is used for anything.
  718.  
  719.   local retval
  720.  
  721.   # validate arguments
  722.   if [ $# -lt 1 ]; then
  723.     usage_error "run() called with wrong number of arguments; expected at" \
  724.                 "least 1, got $#"
  725.     exit $SHELL_LIB_USAGE_ERROR
  726.   fi
  727.  
  728.   "$@" || retval=$?
  729.  
  730.   if [ ${retval:-0} -ne 0 ]; then
  731.     observe "command \"$*\" exited with status $retval"
  732.   fi
  733. }
  734.  
  735. register_x_lib_dir_with_ld_so () {
  736.   # syntax: register_x_lib_dir_with_ld_so
  737.   #
  738.   # Configure the dynamic loader ld.so to search /usr/X11R6/lib for shared
  739.   # libraries.
  740.   #
  741.   # Call this function from the postinst script of a package that places a
  742.   # shared library in /usr/X11R6/lib, before invoking ldconfig.
  743.  
  744.   local dir ldsoconf
  745.  
  746.   dir="/usr/X11R6/lib"
  747.   ldsoconf="/etc/ld.so.conf"
  748.  
  749.   # is the line not already present?
  750.   if ! fgrep -qsx "$dir" "$ldsoconf"; then
  751.     observe "adding $dir directory to $ldsoconf"
  752.     echo "$dir" >> "$ldsoconf"
  753.   fi
  754. }
  755.  
  756. deregister_x_lib_dir_with_ld_so () {
  757.   # syntax: deregister_x_lib_dir_with_ld_so
  758.   #
  759.   # Configure dynamic loader ld.so to not search /usr/X11R6/lib for shared
  760.   # libraries, if and only if no shared libaries remain there.
  761.   #
  762.   # Call this function from the postrm script of a package that places a shared
  763.   # library in /usr/X11R6/lib, in the event "$1" is "remove", and before
  764.   # invoking ldconfig.
  765.  
  766.   local dir ldsoconf fgrep_status cmp_status
  767.  
  768.   dir="/usr/X11R6/lib"
  769.   ldsoconf="/etc/ld.so.conf"
  770.  
  771.   # is the line present?
  772.   if fgrep -qsx "$dir" "$ldsoconf"; then
  773.     # are there any shared objects in the directory?
  774.     if [ "$(echo "$dir"/lib*.so.*.*)" = "$dir/lib*.so.*.*" ]; then
  775.       # glob expansion produced nothing, so no shared libraries are present
  776.       observe "removing $dir directory from $ldsoconf"
  777.       # rewrite the file (very carefully)
  778.       set +e
  779.       fgrep -svx "$dir" "$ldsoconf" > "$ldsoconf.dpkg-tmp"
  780.       fgrep_status=$?
  781.       set -e
  782.       case $fgrep_status in
  783.         0|1) ;; # we don't actually care if any lines matched or not
  784.         *) die "error reading \"$ldsoconf\"; fgrep exited with status" \
  785.           "$fgrep_status" ;;
  786.       esac
  787.       set +e
  788.       cmp -s "$ldsoconf.dpkg-tmp" "$ldsoconf"
  789.       cmp_status=$?
  790.       set -e
  791.       case $cmp_status in
  792.         0) rm "$ldsoconf.dpkg-tmp" ;; # files are identical
  793.         1) mv "$ldsoconf.dpkg-tmp" "$ldsoconf" ;; # files differ
  794.         *) die "error comparing \"$ldsoconf.dpkg-tmp\" to \"$ldsoconf\";" \
  795.           "cmp exited with status $cmp_status" ;;
  796.       esac
  797.     fi
  798.   fi
  799. }
  800.  
  801. make_symlink_sane () {
  802.   # syntax: make_symlink_sane symlink target
  803.   #
  804.   # Ensure that the symbolic link symlink exists, and points to target.
  805.   #
  806.   # If symlink does not exist, create it and point it at target.
  807.   #
  808.   # If symlink exists but is not a symbolic link, back it up.
  809.   #
  810.   # If symlink exists, is a symbolic link, but points to the wrong location, fix
  811.   # it.
  812.   #
  813.   # If symlink exists, is a symbolic link, and already points to target, do
  814.   # nothing.
  815.   #
  816.   # This function wouldn't be needed if ln had an -I, --idempotent option.
  817.  
  818.   # Validate arguments.
  819.   if [ $# -ne 2 ]; then
  820.     usage_error "make_symlink_sane() called with wrong number of arguments;" \
  821.       "expected 2, got $#"
  822.     exit $SHELL_LIB_USAGE_ERROR
  823.   fi
  824.  
  825.   # We could just use the positional parameters as-is, but that makes things
  826.   # harder to follow.
  827.   local symlink target
  828.  
  829.   symlink="$1"
  830.   target="$2"
  831.  
  832.   if [ -L "$symlink" ] && [ "$(readlink "$symlink")" = "$target" ]; then
  833.       observe "link from $symlink to $target already exists"
  834.   else
  835.     observe "creating symbolic link from $symlink to $target"
  836.     mkdir -p "${target%/*}" "${symlink%/*}"
  837.     ln -s -b -S ".dpkg-old" "$target" "$symlink"
  838.   fi
  839. }
  840.  
  841. migrate_dir_to_symlink () {
  842.   # syntax: migrate_dir_to_symlink old_location new_location
  843.   #
  844.   # Per Debian Policy section 6.5.4, "A directory will never be replaced by a
  845.   # symbolic link to a directory or vice versa; instead, the existing state
  846.   # (symlink or not) will be left alone and dpkg will follow the symlink if
  847.   # there is one."
  848.   #
  849.   # We have to do it ourselves.
  850.   #
  851.   # This function moves the contents of old_location, a directory, into
  852.   # new_location, a directory, then makes old_location a symbolic link to
  853.   # new_location.
  854.   #
  855.   # old_location need not exist, but if it does, it must be a directory (or a
  856.   # symlink to a directory).  If it is not, it is backed up.  If new_location
  857.   # exists already and is not a directory, it is backed up.
  858.   #
  859.   # This function should be called from a package's preinst so that other
  860.   # packages unpacked after this one --- but before this package's postinst runs
  861.   # --- are unpacked into new_location even if their payloads contain
  862.   # old_location filespecs.
  863.  
  864.   # Validate arguments.
  865.   if [ $# -ne 2 ]; then
  866.     usage_error "migrate_dir_to_symlink() called with wrong number of"
  867.                 "arguments; expected 2, got $#"
  868.     exit $SHELL_LIB_USAGE_ERROR
  869.   fi
  870.  
  871.   # We could just use the positional parameters as-is, but that makes things
  872.   # harder to follow.
  873.   local new old
  874.  
  875.   old="$1"
  876.   new="$2"
  877.  
  878.   # Is old location a symlink?
  879.   if [ -L "$old" ]; then
  880.     # Does it already point to new location?
  881.     if [ "$(readlink "$old")" = "$new" ]; then
  882.       # Nothing to do; migration has already been done.
  883.       observe "migration of $old to $new already done"
  884.       return 0
  885.     else
  886.       # Back it up.
  887.       warn "backing up symbolic link $old as $old.dpkg-old"
  888.       mv -b "$old" "$old.dpkg-old"
  889.     fi
  890.   fi
  891.  
  892.   # Does old location exist, but is not a directory?
  893.   if [ -e "$old" ] && ! [ -d "$old" ]; then
  894.       # Back it up.
  895.       warn "backing up non-directory $old as $old.dpkg-old"
  896.       mv -b "$old" "$old.dpkg-old"
  897.   fi
  898.  
  899.   observe "migrating $old to $new"
  900.  
  901.   # Is new location a symlink?
  902.   if [ -L "$new" ]; then
  903.     # Does it point the wrong way, i.e., back to where we're migrating from?
  904.     if [ "$(readlink "$new")" = "$old" ]; then
  905.       # Get rid of it.
  906.       observe "removing symbolic link $new which points to $old"
  907.       rm "$new"
  908.     else
  909.       # Back it up.
  910.       warn "backing up symbolic link $new as $new.dpkg-old"
  911.       mv -b "$new" "$new.dpkg-old"
  912.     fi
  913.   fi
  914.  
  915.   # Does new location exist, but is not a directory?
  916.   if [ -e "$new" ] && ! [ -d "$new" ]; then
  917.     warn "backing up non-directory $new as $new.dpkg-old"
  918.     mv -b "$new" "$new.dpkg-old"
  919.   fi
  920.  
  921.   # Create new directory if it does not yet exist.
  922.   if ! [ -e "$new" ]; then
  923.     observe "creating $new"
  924.     mkdir -p "$new"
  925.   fi
  926.  
  927.   # Copy files in old location to new location.  Back up any filenames that
  928.   # already exist in the new location with the extension ".dpkg-old".
  929.   observe "copying files from $old to $new"
  930.   if ! (cd "$old" && cp -a -b -S ".dpkg-old" . "$new"); then
  931.     die "error(s) encountered while copying files from $old to $new"
  932.   fi
  933.  
  934.   # Remove files at old location.
  935.   observe "removing $old"
  936.   rm -r "$old"
  937.  
  938.   # Create symlink from old location to new location.
  939.   make_symlink_sane "$old" "$new"
  940. }
  941.  
  942. # vim:set ai et sw=2 ts=2 tw=80:
  943.  
  944. # GOBSTOPPER: The X Strike Force shell library ends here.
  945.  
  946. CONFIG_DIR=/etc/X11
  947. SERVER_SYMLINK="$CONFIG_DIR/X"
  948. XF86CONFIG="$CONFIG_DIR/XF86Config-4"
  949. XORGCONFIG="$CONFIG_DIR/xorg.conf"
  950. CONFIG_AUX_DIR=/var/lib/x11
  951. SERVER_SYMLINK_CHECKSUM="$CONFIG_AUX_DIR/${SERVER_SYMLINK##*/}.md5sum"
  952. SERVER_SYMLINK_ROSTER="$CONFIG_AUX_DIR/${SERVER_SYMLINK##*/}.roster"
  953. XF86CONFIG_CHECKSUM="$CONFIG_AUX_DIR/${XF86CONFIG##*/}.md5sum"
  954. XORGCONFIG_CHECKSUM="$CONFIG_AUX_DIR/${XORGCONFIG##*/}.md5sum"
  955. XF86CONFIG_ROSTER="$CONFIG_AUX_DIR/${XF86CONFIG##*/}.roster"
  956. XORGCONFIG_ROSTER="$CONFIG_AUX_DIR/${XORGCONFIG##*/}.roster"
  957. THIS_SERVER=/usr/bin/Xorg
  958.  
  959. # clean up non-conffile configuration files and related materials on purge
  960. if [ "$1" = "purge" ]; then
  961.   rm -f "$SERVER_SYMLINK"
  962.  
  963.   # de-register this file as a handler of the Xorg 4.x X server configuration
  964.   # file
  965.   if [ -e "$XORGCONFIG_ROSTER" ]; then
  966.     # check existing roster file for our package name
  967.     if fgrep -qx "$THIS_PACKAGE" "$XORGCONFIG_ROSTER"; then
  968.       # construct temporary roster file with our package name removed, ignoring
  969.       # failure
  970.       fgrep -vx "$THIS_PACKAGE" "$XORGCONFIG_ROSTER" > \
  971.         "$XORGCONFIG_ROSTER.dpkg-tmp" || true
  972.       # is there anything left?
  973.       if [ -s "$XORGCONFIG_ROSTER.dpkg-tmp" ]; then
  974.         # yes, replace the roster file
  975.         mv "$XORGCONFIG_ROSTER.dpkg-tmp" "$XORGCONFIG_ROSTER"
  976.       else
  977.         # no; remove both the roster and our temporary copy
  978.         rm -f "$XORGCONFIG_ROSTER" "$XORGCONFIG_ROSTER.dpkg-tmp"
  979.         # remove Xorg X server configuration file if it was still
  980.         # managed by the package
  981.         if [ -e "$XORGCONFIG_CHECKSUM" ]; then
  982.           # does it exist?
  983.           if [ -e "$XORGCONFIG" ]; then
  984.             # does the current MD5 checksum match the stored checksum?
  985.             if [ "$(md5sum "$XORGCONFIG")" \
  986.                  = "$(cat "$XORGCONFIG_CHECKSUM")" ]; then
  987.               # yes; remove the config file
  988.               rm -f "$XORGCONFIG"
  989.             fi
  990.           fi
  991.           # remove the checksum file; any remaining XFree86 4.x X server
  992.           # configuration file still on the system at this point is no longer
  993.           # being managed (local user customization)
  994.           rm -f "$XORGCONFIG_CHECKSUM"
  995.         fi
  996.       fi
  997.     fi
  998.   fi
  999.  
  1000.   for DIR in "$CONFIG_DIR" "$CONFIG_AUX_DIR"; do
  1001.       rmdir "$DIR" 2> /dev/null || true
  1002.   done
  1003. fi
  1004.  
  1005. case "$1" in
  1006.   abort-install|abort-upgrade)
  1007.     if dpkg --compare-versions "$2" lt-nl "1:7.3+11"; then
  1008.       remove_conffile_rollback "/etc/init.d/xserver-xorg"
  1009.     fi
  1010.     ;;
  1011. esac
  1012.  
  1013. # Automatically added by dh_installdebconf
  1014. if [ "$1" = purge ] && [ -e /usr/share/debconf/confmodule ]; then
  1015.     . /usr/share/debconf/confmodule
  1016.     db_purge
  1017. fi
  1018. # End automatically added section
  1019.  
  1020.  
  1021. exit 0
  1022.  
  1023. # vim:set ai et sts=2 sw=2 tw=0:
  1024.